﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using System.Security.Cryptography;
using System.IO;

namespace Job.Core
{
    /// <summary>
    /// cookie操作类
    /// </summary>
    public class CookieHelper
    {
        public static string BaseDomainName = "abs100.com";
        public static string SecretKey = "ysycovusjobde3Core";

        /// <summary>
        /// 
        /// </summary>
        public static string GetCookie(string strCookieName)
        {
            return GetCookie(strCookieName, true, SecretKey);
        }

        /// <summary>
        /// 
        /// </summary>
        public static string GetCookie(string strCookieName, bool NeedDecrypt, string strSecretKey)
        {
            HttpRequest request = System.Web.HttpContext.Current.Request;
            HttpCookie cookie = request.Cookies[strCookieName];
            if (NeedDecrypt)
                return (cookie != null ? SecretHelper.Decrypt(cookie.Value, strSecretKey) : string.Empty);
            else
                return cookie != null ? cookie.Value : ""; ;
        }

        /// <summary>
        /// 设置cookie
        /// </summary>
        public static void SetCookie(string strCookieName, string strValue, int intDays)
        {
            SetCookie(strCookieName, strValue, intDays, BaseDomainName, SecretKey);
        }

        /// <summary>
        /// 设置COOKIE, 可自定义网站主域
        /// </summary>
        public static void SetCookie(string strCookieName, string strValue, int intDays, string strDomain, string strSecretKey)
        {
            HttpResponse response = System.Web.HttpContext.Current.Response;
            HttpCookie cookie = new HttpCookie(strCookieName);
            cookie.Value = SecretHelper.Encrypt(strValue, SecretKey);
            cookie.Expires = DateTime.Now.AddDays(intDays);
            cookie.Domain = strDomain;
            response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 
        /// </summary>
        public static void DeleteCookie(string strCookieName)
        {
            HttpResponse response = System.Web.HttpContext.Current.Response;
            HttpCookie cookie = new HttpCookie(strCookieName);
            cookie.Expires = DateTime.Now.AddDays(-30);
            response.Cookies.Add(cookie);
        }
    }
}
